(function(document){
	
	function SlidePage(){
		
		
		
		
	}
	
	
	function SlidePanel(){
		//屏幕宽高信息
		var winW = window.innerWidth;
		var winH = window.innerHeight;
		//检查是否支持动画
		var has3d = (navigator.appVersion.search(/android/i) == -1);
		//移动对象
		var moveEle;
		//
		var currPanelIndex = 0;
		
		var boxList = [];
		
		var touchSpeedInfo = {
			a:0,//加速度
			u:0.3,//摩擦系数
			st:0,//开始时间
			et:0 //结束事件
		};
		
		//移动方向
		var SLIDE_TYPE = {
			top:1,
			right:2,
			bottom:3,
			left:4
		}
		//移动位置
		var offset = {
			x:0,
			y:0
		}
		var touchInfo = {
			orgP : {
				x : 0,
				y:0
			},
			aimP : {
				x : 0,
				y:0
			},
			moved :{
				x:0,
				y:0
			},
			getTouchLength:function(){
				return {x:this.aimP.x-this.orgP.x,y:this.aimP.y-this.orgP.y}
			},
			getForward:function(){
				var flag = {};
				var touchInfo = this.getTouchLength();
				if(touchInfo.x>0){
					flag.x=1;
				}else if(touchInfo.x<0){
					flag.x=-1;
				}else{
					flag.x=0;
				}
				if(touchInfo.y>0){
					flag.y=1;
				}else if(touchInfo.y<0){
					flag.y=-1;
				}else{
					flag.y=0;
				}
				return flag;
			}
			
		}
		
		
		/*-------------------------------内部方法  ---------------------------*/
		
		function $(s){
			return document.getElementById(s);
		}
		// 根据class得到元素
		function $class(s) {
			return document.getElementsByClassName(s);
		}
		function _init(element){
			//得到滑动对象
			var panelDom = $(element);
			moveEle = panelDom.firstElementChild;
			//初始化容器尺寸
			_initSize(panelDom);
			if(!moveEle){
				console.log("移动元素未得到");
			}
			
			_initTouchEvent();
		}
		
		function _initSize(panelDom){
			panelDom.style.width = winW+"px";
			panelDom.style.height = winH+"px";
			var allBoxs = panelDom.firstElementChild.children;
			for(var i=0,n=allBoxs.length;i<n;i++){
				allBoxs[i].style.width = winW+"px";
				boxList.push(allBoxs[i]);
			}
		}
			// 添加touch事件
		var _initTouchEvent = function() {
			
			// 位置基本信息
			
	
	
			document.addEventListener("touchstart",touchStar, false);
	
			document.addEventListener("touchmove",touchMove, false);
	
			document.addEventListener("touchend", touchEnd, false);
	
		}
		
		function touchStar(event){
			var touch = event.touches[0];
			// 设置源对象位置
			touchInfo.orgP.x = touch.screenX;
			touchInfo.orgP.y = touch.screenY;
			touchSpeedInfo.st = new Date();
			
			
		}
		function touchMove(event){
			event.preventDefault();
			var touch = event.touches[0];
			var move = {x:0,y:0};
			touchInfo.aimP.x = touch.screenX;
			touchInfo.aimP.y = touch.screenY;
			var touchLength = touchInfo.getTouchLength();
			
			//左右滑动超过1/3菜判定为左右滑动
			if(Math.abs(touchLength.x)>(winW/3)){
				move.x = touchLength.x+touchInfo.moved.x;
			}else{
				move.x = touchInfo.moved.x;
			}
			//上下滑动超过1/3菜判定为左右滑动
			if(Math.abs(touchLength.y)>(winH/3)){
				move.y = touchLength.y+touchInfo.moved.y;
			}else{
				move.y = touchInfo.moved.y;
			}
			
			_translate(offset.x,offset.y);
			
		
		}
		function touchEnd(event){
			var touchLength = touchInfo.getTouchLength();
			var forward = touchInfo.getForward();
			
			var move={x:0,y:0};
			var tempIndex = currPanelIndex;
			
			
			//如果滑动大于屏幕一半，开始切换，否则复原
			if(Math.abs(touchLength.x)>(winW/2)){
				tempIndex =tempIndex - forward.x;
				move.y=0;
			}else{
				move.y=offset.y;
			}
			
			move.x = -tempIndex*winW;
			move.y = (touchLength.y/200)*winH;
			console.log("x="+move.x+"y:"+move.y);
			
			_translate(move.x,offset.y+move.y);
			
			touchInfo.moved.x = move.x;
			touchInfo.moved.y = move.y;
			
			currPanelIndex = tempIndex;
		}
		
	    function _translate (x,y) {
	    	if(isNaN(x)||isNaN(y)){
	    		console.log("x,y不同为数字");
	    	}
	    	var x = x||0;
	    	var y = y||0;
			if (has3d) {
				moveEle.style.webkitTransform = 'translate3d(' + x+ 'px,' + y+ 'px,0px)';
			} else {
				moveEle.style.webkitTransform = 'translate(' + x + 'px,' + y + 'px)';
			}
			offset.x = x;
			offset.y = y;
			
		}
		
		/*-------------------------------内部方法 /  ---------------------------*/
		
		
		/*-------------------------------对外公开调用方法  ---------------------------*/
		
		/**
		 * 滑动
		 */
		this.slide = function(slideType){
		
		}
		
		this.next = function(){
		
		}
		this.prev = function(){
		
		}
		
		this.init = function(element){
			_init(element);
		}
		
		//transform 性测试
		this.testTransform = function(){
			var flag = -1;
			setInterval(function(){
				_translate (-1*flag*winW,-Math.random()*300);
				flag = -1*flag;
			},1000)
		}
		/*-------------------------------对外公开调用方法  /---------------------------*/
		
	}
	
	
	var slidePanel = new SlidePanel();
	slidePanel.init("slidePange");
	//slidePanel.testTransform();
	window.slide = slidePanel;
})(document);